package org.csu.mypetstore.web.servlets;

import org.csu.mypetstore.domain.Account;
import org.csu.mypetstore.service.AccountService;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

public class NewAccountServlet extends HttpServlet {
    private static final String main = "/WEB-INF/jsp/catalog/Main.jsp";
    private static final String ERROR = "/WEB-INF/jsp/common/Error.jsp";
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request,response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException,IOException{
        HttpSession session = request.getSession();
        AccountService accountService = new AccountService();
        Account account = new Account();
        String userId = request.getParameter("username");
        String password = request.getParameter("password");
        String firstName = request.getParameter("account.firstName");
        String lastName = request.getParameter("account.lastName");
        String email = request.getParameter("account.email");
        String phone = request.getParameter("account.phone");
        String address1 = request.getParameter("account.address1");
        String address2 = request.getParameter("account.address2");
        String city = request.getParameter("account.city");
        String state = request.getParameter("account.state");
        String zip = request.getParameter("account.zip");
        String country = request.getParameter("account.country");
        String languagePerference = request.getParameter("account.languagePreference");
        String favouriteCategoryId = request.getParameter("account.favouriteCategoryId");
        account.setUsername(userId);
        account.setPassword(password);
        account.setFirstName(firstName);
        account.setLastName(lastName);
        account.setEmail(email);
        account.setPhone(phone);
        account.setAddress1(address1);
        account.setAddress2(address2);
        account.setCity(city);
        account.setState(state);
        account.setZip(zip);
        account.setCountry(country);
        account.setLanguagePreference(languagePerference);
        account.setFavouriteCategoryId(favouriteCategoryId);
        account.setListOption(request.getParameter("account.listOption")!= null);
        account.setBannerOption(request.getParameter("account.bannerOption")!=null);
        if (accountService.getAccount(userId)!=null){
            session.setAttribute("message","The user name already exists.");
            request.getRequestDispatcher(ERROR).forward(request,response);
        }
        else {
            accountService.insertAccount(account);
            session.setAttribute("account", null);
            request.getRequestDispatcher(main).forward(request, response);
        }
    }
}
